package reservations

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/reservations"
	reservationsReq "github.com/flipped-aurora/gin-vue-admin/server/model/reservations/request"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ReservationApi struct{}

// CreateReservation 创建reservation表
// @Tags Reservation
// @Summary 创建reservation表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body reservations.Reservation true "创建reservation表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /reservation/createReservation [post]
func (reservationApi *ReservationApi) CreateReservation(c *gin.Context) {
	var reservation reservations.Reservation
	err := c.ShouldBindJSON(&reservation)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	reservation.CreatedBy = utils.GetUserID(c)
	err = reservationService.CreateReservation(&reservation)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteReservation 删除reservation表
// @Tags Reservation
// @Summary 删除reservation表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body reservations.Reservation true "删除reservation表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /reservation/deleteReservation [delete]
func (reservationApi *ReservationApi) DeleteReservation(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	err := reservationService.DeleteReservation(ID, userID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteReservationByIds 批量删除reservation表
// @Tags Reservation
// @Summary 批量删除reservation表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /reservation/deleteReservationByIds [delete]
func (reservationApi *ReservationApi) DeleteReservationByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	err := reservationService.DeleteReservationByIds(IDs, userID)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateReservation 更新reservation表
// @Tags Reservation
// @Summary 更新reservation表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body reservations.Reservation true "更新reservation表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /reservation/updateReservation [put]
func (reservationApi *ReservationApi) UpdateReservation(c *gin.Context) {
	var reservation reservations.Reservation
	err := c.ShouldBindJSON(&reservation)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	reservation.UpdatedBy = utils.GetUserID(c)
	err = reservationService.UpdateReservation(reservation)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindReservation 用id查询reservation表
// @Tags Reservation
// @Summary 用id查询reservation表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询reservation表"
// @Success 200 {object} response.Response{data=reservations.Reservation,msg=string} "查询成功"
// @Router /reservation/findReservation [get]
func (reservationApi *ReservationApi) FindReservation(c *gin.Context) {
	ID := c.Query("ID")
	rereservation, err := reservationService.GetReservation(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(rereservation, c)
}

// GetReservationList 分页获取reservation表列表
// @Tags Reservation
// @Summary 分页获取reservation表列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query reservationsReq.ReservationSearch true "分页获取reservation表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /reservation/getReservationList [get]
func (reservationApi *ReservationApi) GetReservationList(c *gin.Context) {
	var pageInfo reservationsReq.ReservationSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := reservationService.GetReservationInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetReservationPublic 不需要鉴权的reservation表接口
// @Tags Reservation
// @Summary 不需要鉴权的reservation表接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /reservation/getReservationPublic [get]
func (reservationApi *ReservationApi) GetReservationPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	reservationService.GetReservationPublic()
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的reservation表接口信息",
	}, "获取成功", c)
}
